from django.http import JsonResponse
from django.views.generic import View

from account.models import UserInfo
from .models import Follow


# Create your views here.

class FollowSomeone(View):
    def get(self, request):
        user_id = request.GET.get("user_id")
        who = UserInfo.objects.filter(id=user_id).first()  # 我要关注的是who
        i_follow = Follow.objects.filter(user=request.universe.user, follow=who).first()
        follow_me = Follow.objects.filter(user=who, follow=request.universe.user).first()
        if i_follow:    # 我关注了该用户，取消关注
            Follow.unfollow_someone(from_user=request.universe.user, to_user=who)
            if follow_me:
                return JsonResponse({
                    'status': True,
                    'data': '成功取关',
                    'friendship': 2,
                })
            else:
                return JsonResponse({
                    'status': True,
                    'data': '成功取关',
                    'friendship': 0,
                })
        else:   # 我没有关注该用户，关注Ta
            Follow.follow_someone(from_user=request.universe.user, to_user=who)
            # Follow.objects.create()
            if follow_me:
                return JsonResponse({
                    'status': True,
                    'data': '成功关注',
                    'friendship': 3,
                })
            else:
                return JsonResponse({
                    'status': True,
                    'data': '成功关注',
                    'friendship': 1,
                })